package com.team233.zuccecho.service.impl;

import com.team233.zuccecho.Bean.Question;
import com.team233.zuccecho.Bean.Teacher;
import com.team233.zuccecho.Bean.Template;
import com.team233.zuccecho.dto.QuestionDto;
import com.team233.zuccecho.dto.TemplateDto;
import com.team233.zuccecho.exception.EchoServiceException;
import com.team233.zuccecho.rabbitMQ.ZuccEchoMessage;
import com.team233.zuccecho.repository.QuestionRepository;
import com.team233.zuccecho.repository.TeacherRepository;
import com.team233.zuccecho.repository.TemplateRepository;
import com.team233.zuccecho.service.inter.TemplateInterface;
import com.team233.zuccecho.util.Constants;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class TemplateImpl implements TemplateInterface {
    @Autowired
    private TeacherRepository teacherRepository;
    @Autowired
    private TemplateRepository templateRepository;
    @Autowired
    private QuestionRepository questionRepository;
    @Autowired
    private AmqpTemplate mqService;

    @Override
    public List<String> getTemplatesBrief(String teacher_id) throws EchoServiceException{
        List<Template> templates = templateRepository.findByTeacherId(teacher_id);
        List<String> templatesInfo = new ArrayList<>();
        if(templates == null){
            templatesInfo.add(new String("该教师暂未创建模板"));
        }else{
            for(Template t:templates){
                templatesInfo.add(t.briefInfo());
            }
            return templatesInfo;
        }
        return null;
    }

    @Override
    public String getSpeTemplate(long template_id) throws EchoServiceException{
        Template template = templateRepository.findByTemplateId(template_id);
        if(template == null){
            throw new EchoServiceException("未找到模板");
        }
        return template.toString();
    }

    @Override
    public Long addTemplate(TemplateDto templateDto) throws EchoServiceException{
        System.out.println("template dto :"+templateDto.getTemplate_name());
        //检查
        if(templateDto.getTemplate_id() != null){

//            throw new EchoServiceException("新建模板不能上传id");
            return null;
        }
        System.out.println(templateDto.getQuestionSet().toString());
        if(templateDto.getQuestionSet() != null){
            templateDto.getQuestionSet().stream().forEach(
                    question -> {
                        if(question.getContent().equals("")){
                            throw new EchoServiceException(question.getQuestionID()+"问题内容不能为空");
                        }
                        if(question.getQuestionType() == 1 || question.getQuestionType() == 2){
                            if(question.getAnswerlist().equals("")){
                                throw new EchoServiceException(question.getQuestionID()+"选择题必须要有选项");
                            }
                        }
                        if(question.getQuestionType() == null){
                            throw new EchoServiceException(question.getQuestionID()+"问卷类型不能为空");
                        }
                    }
            );
        }
        //保存
        Template template = new Template();
        Teacher teacher = teacherRepository.findTeaById(templateDto.getTea_id());
        List<Question> questions = new ArrayList<>();
        for(QuestionDto questionDto:templateDto.getQuestionSet()){
            Question question = new Question();
            question.setQuestionType(questionDto.getQuestionType());
            question.setContent(questionDto.getContent());
            question.setAnswerList(questionDto.getAnswerlist());
            questionRepository.save(question);
            questions.add(question);
        }
        template.setTeacher(teacher);
        template.setIf_delete(0);
        template.setQuestionSet(questions);
        template.setTemplate_name(templateDto.getTemplate_name());
        template.setCreate_time(new Timestamp(Calendar.getInstance().getTimeInMillis()));
        templateRepository.save(template);
        return template.getTemplate_id();
    }

    @Override
    public Long modifyTemplate(TemplateDto templateDto) {
        questionRepository.removeTemQsById(templateDto.getTemplate_id());
        Template template = templateRepository.findByTemplateId(templateDto.getTemplate_id());
        List<Question> questions = new ArrayList<>();
        for(QuestionDto questionDto:templateDto.getQuestionSet()){
            Question question = new Question();
            question.setQuestionType(questionDto.getQuestionType());
            question.setContent(questionDto.getContent());
            question.setAnswerList(questionDto.getAnswerlist());
            questionRepository.save(question);
            questions.add(question);
        }
        template.setQuestionSet(questions);
        template.setTemplate_name(templateDto.getTemplate_name());
        template.setModify_time(new Timestamp(Calendar.getInstance().getTimeInMillis()));
        templateRepository.save(template);
        return template.getTemplate_id();
    }

    @Override
    public Integer deleteTemplate(TemplateDto templateDto) {
        if(templateDto.getTemplate_id() == null){
            throw new EchoServiceException("删除模板id不能为空");
        }
        Template template = templateRepository.getById(templateDto.getTemplate_id());
        template.setIf_delete(1);
        templateRepository.save(template);
        return 1;
    }
}
